﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Apollo.Web;
using Apollo.Core;
using Apollo.Modules.Commissioning;
using Apollo;

namespace Apollo.Website
{
    public partial class Controls_Edit_FormItemCommissionCalculation : System.Web.UI.UserControl, IFormItem
    {
        public FormItem Item { get; set; }

        public object TargetObject { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            DropDownListCommissionCalculationBase.Attributes.Add("onchange", "ChangeCommissionCalculation(this, false)");

            if (!this.Page.IsPostBack)
            {
                ICommissionCalculationBase objCA = this.TargetObject as ICommissionCalculationBase;

                if (objCA == null)
                    return;

                foreach (ListItem li in this.DropDownListCommissionCalculationBase.Items)
                    li.Selected = li.Value == ((int)objCA.CalculationBase).ToString();

                this.CheckBoxXPMed.Checked = objCA.UseExtraPremiumMedical;
                this.CheckBoxXPProf.Checked = objCA.UseExtraPremiumProfessional;
                this.CheckBoxXPOther.Checked = objCA.UseExtraPremiumOther;
                this.CheckBoxLoading.Checked = objCA.UseLoading;
                this.CheckBoxPayFreq.Checked = objCA.UsePaymentFrequency;
            }
        }


        public ActionMessage Save(object obj)
        {
            try
            {
                ICommissionCalculationBase ca = obj as ICommissionCalculationBase;

                if (ca == null)
                {
                    return new ActionMessage("Commission can not be null!", ActionMessageType.Error);
                }

                ca.CalculationBase = (CommissionCalculationBase)int.Parse(DropDownListCommissionCalculationBase.SelectedValue);

                ca.UseExtraPremiumMedical = CheckBoxXPMed.Checked;
                ca.UseExtraPremiumOther = CheckBoxXPOther.Checked;
                ca.UseExtraPremiumProfessional = CheckBoxXPProf.Checked;
                ca.UseLoading = CheckBoxLoading.Checked;
                ca.UsePaymentFrequency = CheckBoxPayFreq.Checked;
            }
            catch (Exception ex)
            {
                return new ActionMessage(this.Item.Projection.DisplayName + " error: " + ex.Message, ActionMessageType.Error);
            }

            return null;
        }
    }
}